/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.service.beans;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;


/**
 * Class corresponding to the "request" element of the requests_conf.xml file.
 * It allows to associate a search request form with searched business objects fields.
 */
public class JBRequest implements java.io.Serializable
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBRequest.class.getName());

    /** Request name. */
    private String name;

    /** List of JBRessourceRequest. */
    private Map ressourceRequestMap = new HashMap();

    /**
     * Returns the request name.
     * @return name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Returns the list of searched business object beans.
     * @return List of JBRessourceRequest
     */
    public Map getRessourceRequestMap()
    {
        return ressourceRequestMap;
    }

    /**
     * Sets the request name.
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
     * Adds a searched business object bean.
     *
     * @param ress business bean definition to add
     */
    public void addRessourceRequest(JBRessourceRequest ress)
    {
        this.ressourceRequestMap.put(ress.getName().trim().toLowerCase(), ress);
    }

    /**
     * Gets a searched business object bean from its name.
     *
     * @param aname bean name
     *
     * @return business bean definition
     */
    public JBRessourceRequest findRessourceRequestByName(String aname)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = aname.trim().toLowerCase();

        return (JBRessourceRequest) ressourceRequestMap.get(key);
    }

    /**
     * Displays request definition.
     *
     * @return String formatted definition
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("   name = ");
        buf.append(name);
        buf.append(System.getProperty("line.separator"));

        if (ressourceRequestMap != null)
        {
            buf.append("   Map components of ressourceRequestMap = ");
            buf.append(System.getProperty("line.separator"));
            it = ressourceRequestMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("   ...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method
}
